from fake_useragent import UserAgent
from selenium import webdriver

from crawler_core.driver import interface
from crawler_core.exception.driver_exec import DriverException
from lib.sys.logger_factory import LoggerFactory
from lib.sys.sys_config import SystemConfig


class Driver(interface.Driver):
    logger = LoggerFactory.getLogger("Driver", LoggerFactory.LOGIN)

    def __init__(self):
        pass

    def getDriver(self):
        options = webdriver.ChromeOptions()
        options.add_argument('lang=zh_CN.UTF-8')
        options.add_argument('--no-sandbox')
        ua = UserAgent()
        options.add_argument('user-agent="%s"' % ua.random)
        try:
            driver = webdriver.Chrome(SystemConfig.get_chrome_driver_path(), chrome_options=options)
            return driver
        except DriverException:
            Driver.logger.info("Driver -- 获取驱动异常...")
            return
